package com.gitee.sop.sopauth.auth.impl;

import com.alibaba.fastjson.JSON;
import com.gitee.sop.servercommon.constant.Constants;
import com.gitee.sop.sopauth.auth.OAuth2Config;
import com.gitee.sop.sopauth.auth.OAuth2Manager;
import com.gitee.sop.sopauth.auth.OpenUser;
import com.gitee.sop.sopauth.auth.Token;
import com.gitee.sop.sopauth.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * oauth2管理redis实现，这个类跟OAuth2ManagerCache类只能用一个，
 * 如果要用这个类，
 * 1、注释掉OAuth2ManagerCache的@Service。
 * 2、在properties中配置redis
 * 3、启用这个类的@Service
 *
 * @author admin
 */
@Service
public class OAuth2ManagerRedis implements OAuth2Manager {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

    public static String getCodeKey(String code) {
        return Constants.CODE_PREFIX + code;
    }

    public static String getAccessTokenKey(String accessToken) {
        return Constants.ACCESS_TOKEN_PREFIX + accessToken;
    }

    public static String getRefreshTokenKey(String refreshToken) {
        return Constants.REFRESH_TOKEN_PREFIX + refreshToken;
    }

    @Override
    public void addToken(String authCode, String accessToken, String refreshToken, OpenUser authUser) {
        long codeTimeoutSeconds = OAuth2Config.getInstance().getCodeTimeoutSeconds();
        redisTemplate.opsForValue().set(
                getCodeKey(authCode),
                JSON.toJSONString(new Token(accessToken, refreshToken, authUser)),
                // token过期时间
                codeTimeoutSeconds,
                TimeUnit.SECONDS);
    }

    @Override
    public Token getTokenByAuthCode(String authCode) {
        String json = redisTemplate.opsForValue().get(getCodeKey(authCode));
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        return JSON.parseObject(json, Token.class);
    }

    @Override
    public void addAccessToken(String accessToken, String refreshToken, OpenUser authUser) {
        // 存accessToken
//        long expiresIn = OAuth2Config.getInstance().getAccessTokenExpiresIn();
        long expiresIn = OAuth2Config.getInstance().getCodeTimeoutSeconds();
        if (expiresIn > 0) {
            redisTemplate.opsForValue().set(getAccessTokenKey(accessToken), JSON.toJSONString(authUser), expiresIn, TimeUnit.SECONDS);
        } else {
            redisTemplate.opsForValue().set(getAccessTokenKey(accessToken), JSON.toJSONString(authUser));
        }

    }

    @Override
    public void removeAccessToken(String accessToken) {
        String accessTokenKey = getAccessTokenKey(accessToken);
        redisTemplate.delete(accessTokenKey);
    }

    @Override
    public OpenUser getOpenUserByAccessToken(String accessToken) {
        String json = redisTemplate.opsForValue().get(getAccessTokenKey(accessToken));
        if (!StringUtils.isEmpty(json)) {
            return JSON.parseObject(json, OpenUser.class);
        }
        return null;
    }
}
